import React, { useReducer } from "react";

const UseReducerComponent = () => {
    const reducer = (state, action) => {
        switch (action.type) {
            case 'increment':
                return state + 1
            case 'decrement':
                return state - 1
            default:
                return state
        }
    }

    const [count, dispatch] = useReducer(reducer, 0)
    return (
        <div>
            <h1>useReducer</h1>
            <p>{count}</p>
            <button onClick={() => dispatch({ type: 'increment' })}>加数</button>
            <button onClick={() => dispatch({ type: 'decrement' })}>减数</button>
        </div>
    )
}

export default UseReducerComponent